<?php

namespace app\admin\controller\baoan;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 报案备注管理
 *
 * @icon fa fa-circle-o
 */
class Beizhu extends Backend
{

    /**
     * Beizhu模型对象
     * @var \app\admin\model\baoan\Beizhu
     */
    protected $model = null;
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\baoan\Beizhu;
        $admin = $this->auth->getUserInfo();
        $this->assign('admin',$admin);
        $rule = Db::name('auth_group_access')->alias('a')->join('auth_group b','a.group_id = b.id')->where('a.uid',$admin['id'])->field('b.rules,a.group_id')->find();
        $rules = $rule['rules'];
        if($rules == '*'){
            $rules = Db::name('auth_rule')->column('id');
        }else{
            $rules = explode(',',$rule['rules']);
        }
        $this->rule = $rules;
        $this->assignconfig('rule', $rules);
        $this->assign('rules', $rules);

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function search()
    {
        return $this->view->fetch();
    }

    /**
     * 查询是否存在记录
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chaxun(){
        $params = $this->request->post('');
        $where = [];
        if($params['car_num']){
            $where['car_num'] = $params['car_num'];
        }
        if($params['phone']){
            $where['phone'] = $params['phone'];
        }
        if(!$where) $this->error('至少填写一项');
        $info = Db::name('kf_record')->where($where)->where('deletetime',)->find();
        if(!$info){
            if($params['car_num']){
                $baoan_info = Db::name('baoan')->where('car_num',$params['car_num'])->find();
                if(!$baoan_info){
                    $this->error('不存在本车牌号的报案信息，不可添加催办信息');
                }
            }else{
                $this->error('不存在本车牌号的报案信息，不可添加催办信息');
            }
            $data['car_num'] = $params['car_num'];
            $data['phone'] = $params['phone'];
            $data['createtime'] = time();
            $data['updatetime'] = time();
            $id = Db::name('kf_record')->insertGetId($data);
        }else{
            $id = $info['id'];
        }
        $this->success('','',['id'=>$id]);
    }

    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function add($ids = null)
    {
        if (false === $this->request->isPost()) {
            $params = $this->request->get('');
            if(isset($params['id']) && $params['id']){
                $id = $params['id'];
            }else{
                $id = $ids;
            }
            if(!$id) $this->error('参数错误');
            $res = Db::name('kf_record')->where('id',$id)->find();
            if($res['car_num']){
                $row = Db::name('kf_record_list')->alias('a')->join('kf_record b','a.rid = b.id')->where('b.car_num',$res['car_num'])->where('a.deletetime',)->where('b.deletetime',)->order('a.id asc')->field('a.*,b.car_num')->select();
            }else{
                $row = Db::name('kf_record_list')->where('rid',$id)->where('deletetime',)->order('id asc')->select();
            }
            $admin = $this->auth->getUserInfo();
            foreach ($row as &$v){
                $v['type_name'] = $this->get_type()[$v['type']];
                if($v['type'] != 10 && $v['type'] != 11){
                    $v['ld_type'] = $v['ld_type'] == 1 ? '本人' : '他人';
                }else{
                    $v['ld_type'] = '';
                }
                $v['fenpei_name'] = $v['fenpei'] ? Db::name('admin')->where('id',$v['fenpei'])->value('username') : '';
                $v['buchang_img'] = $v['buchang_img'] ? explode(',',$v['buchang_img']) : [];
                $v['cuiban_img'] = $v['cuiban_img'] ? explode(',',$v['cuiban_img']) : [];
                $v['is_ben'] = 0;
                if($v['fenpei'] == $admin['id']){
                    $v['is_ben'] = 1;
                }
            }

            $baoan_info = Db::name('baoan')->where('car_num',$res['car_num'])->find();
            $res['is_add'] = 1;
            if(!$baoan_info){
                $res['is_add'] = 0;
            }
            $this->view->assign('res',$res);
            $this->view->assign('row',$row);
            $this->view->assign('admin',$admin);

            $cateid = $admin['zuzhi'];
            $date = db('zuzhi')->where('delete_id',0)->select();
            $arr = $this->frontdels($date,$cateid);
            $arr[] = (int)$cateid;
            $fenpei = Db::name('admin')
                ->alias('a')
                ->join("auth_group_access b", "a.id = b.uid")
                ->where('a.zuzhi','in',$arr)
                ->where('b.group_id', 10)
                ->field('a.username,a.id')->select();
            $this->view->assign('fenpei',$fenpei);
            return $this->view->fetch();
        }
    }

    /**
     * 保存客服记录
     * @return void
     */
    public function save_list(){
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $result = false;
        $row = [];
        Db::startTrans();
        try {
            $admin = $this->auth->getUserInfo();
            if(isset($params['id']) && $params['id']){
                $data['buchang_img'] = $params['buchang_img'];
                $data['buchang_beizhu'] = $params['buchang_beizhu'];
                $data['buchang_date'] = date('Y-m-d H:i:s');
                $result = Db::name('kf_record_list')->where('id',$params['id'])->update($data);
                $row = Db::name('kf_record_list')->where(['id'=>$params['id']])->find();
            }else{
                $data['rid'] = $params['rid'];
                $data['type'] = $params['type'];
                $data['fenpei'] = $params['fenpei'];
                $data['user_id'] = $admin['id'];
                $data['user_name'] = $admin['username'];
                $data['cuiban_img'] = $params['cuiban_img'];
                $data['cuiban_beizhu'] = $params['cuiban_beizhu'];
                $data['date'] = date('Y-m-d H:i:s');
                $data['is_read'] = 1;
                $result = Db::name('kf_record_list')->insertGetId($data);
                $row = Db::name('kf_record_list')->where(['id'=>$result])->find();
            }
            $row['cuiban_img'] = $row['cuiban_img'] ? explode(',',$row['cuiban_img']) : [];
            $row['buchang_img'] = $row['buchang_img'] ? explode(',',$row['buchang_img']) : [];
            $row['fenpei'] = $row['fenpei'] ? Db::name('admin')->where('id',$row['fenpei'])->value('username') : '';
            $row['is_ben'] = 0;
            if($row['fenpei'] == $admin['id']){
                $row['is_ben'] = 1;
            }
            $row['type'] = $this->get_type()[$row['type']];
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success('','',$row);
    }

    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function adds($ids = null)
    {
        if (false === $this->request->isPost()) {
            $params = $this->request->get('');
            if(isset($params['id']) && $params['id']){
                $id = $params['id'];
            }else{
                $id = $ids;
            }
            Db::name('kf_record_list')->where('rid',$id)->update(['is_read'=>1]);
            $list_ids = Db::name('kf_record_list')->where('rid',$id)->column('id');
            Db::name('fastchat_record')->whereIn('kf_id',$list_ids)->update(['status'=>1]);
            if(!$id) $this->error('参数错误');
            $res = Db::name('kf_record')->where('id',$id)->find();
            if($res['car_num']){
                $row = Db::name('kf_record_list')->alias('a')->join('kf_record b','a.rid = b.id')->where('b.car_num',$res['car_num'])->where('a.deletetime',)->where('b.deletetime',)->order('a.id asc')->field('a.*,b.car_num')->select();
            }else{
                $row = Db::name('kf_record_list')->where('rid',$id)->where('deletetime',)->order('id asc')->select();
            }
            $admin = $this->auth->getUserInfo();
            foreach ($row as &$v){
                $v['type_name'] = $this->get_type()[$v['type']];
                $v['ld_type'] = $v['ld_type'] == 1 ? '本人' : '他人';
                $v['fenpei_name'] = $v['fenpei'] ? Db::name('admin')->where('id',$v['fenpei'])->value('username') : '';
                $v['buchang_img'] = $v['buchang_img'] ? explode(',',$v['buchang_img']) : [];
                $v['cuiban_img'] = $v['cuiban_img'] ? explode(',',$v['cuiban_img']) : [];
                $v['is_ben'] = 0;
                if($v['fenpei'] == $admin['id']){
                    $v['is_ben'] = 1;
                }
            }

            $baoan_info = Db::name('baoan')->where('car_num',$res['car_num'])->find();
            $res['is_add'] = 1;
            if(!$baoan_info){
                $res['is_add'] = 0;
            }

            $this->view->assign('res',$res);
            $this->view->assign('row',$row);
            $admin = $this->auth->getUserInfo();
            $this->view->assign('admin',$admin);

            $cateid = $admin['zuzhi'];
            $date = db('zuzhi')->where('delete_id',0)->select();
            $arr = $this->frontdels($date,$cateid);
            $arr[] = (int)$cateid;
            $fenpei = Db::name('admin')
                ->alias('a')
                ->join("auth_group_access b", "a.id = b.uid")
                ->where('a.zuzhi','in',$arr)
                ->where('b.group_id', 10)
                ->field('a.username,a.id')->select();
            $this->view->assign('fenpei',$fenpei);
            return $this->view->fetch('add');
        }
    }

    public function get_type(){
        return ['1'=>'报案','2'=>'咨询','3'=>'退费','4'=>'销案退费','5'=>'客服催办','6'=>'投诉','7'=>'工商投诉','8'=>'公众号投诉','9'=>'支付投诉','10'=>'渠道催办','11'=>'舆情处理'];
    }
    public function huifu_tan(){
        $id = $_GET['id'];
        $row = Db::name('kf_record_list')->where('id',$id)->find();
        $row['type'] = $this->get_type()[$row['type']];
        $row['ld_type'] = $row['ld_type'] == 1 ? '本人' : '他人';
        $row['fenpei'] = $row['fenpei'] ? Db::name('admin')->where('id',$row['fenpei'])->value('username') : '';
        $row['cuiban_img'] = $row['cuiban_img'] ? explode(',',$row['cuiban_img']) : [];
        $this->view->assign('row',$row);
        $info = Db::name('kf_record')->where('id',$row['rid'])->find();
        $info['learned_name'] = '';
        if($info['one_id']){
            $info['learned_name'] = Db::name('list_one')->where('id',$info['one_id'])->value('learned_name');
        }

        $this->view->assign('info',$info);
        $site = Config::get('site');
        $this->view->assign('chexing',$site['chexing']);
        return $this->fetch();
    }


    public function tan(){
        $id = $_GET['id'];
        $type = $_GET['type'];
        $row = Db::name('kf_record')->where('id',$id)->find();

        if($row['car_num']){
            $baoan_info = Db::name('baoan')->where('car_num',$row['car_num'])->find();
            if(!$baoan_info){
                $this->error('不存在本车牌号的报案信息，不可添加催办信息');
            }
        }else{
            $this->error('不存在本车牌号的报案信息，不可添加催办信息');
        }
        $this->view->assign('row',$row);
        $admin = $this->auth->getUserInfo();
        $this->view->assign('admin',$admin);

        $cateid = $admin['zuzhi'];
        $date = db('zuzhi')->where('delete_id',0)->select();
        $arr = $this->frontdels($date,$cateid);
        $arr[] = (int)$cateid;
        $fenpei = Db::name('admin')
            ->alias('a')
            ->join("auth_group_access b", "a.id = b.uid")
            ->where('a.zuzhi','in',$arr)
            ->where('b.group_id', 10)
            ->field('a.username,a.id')->select();
        $this->view->assign('fenpei',$fenpei);
        $this->view->assign('type',$type);
        return $this->fetch();
    }
    public function frontdels($data,$cateid){
        //var_dump($data);die();
        static $arr=array();
        foreach ($data as $key => $v) {
            if($v["pid"]==$cateid){
                $arr[]=$v['id'];
                $this->frontdels($data,$v['id']);
            }
        }
        return $arr;
    }

}
